package com.syy.api.system.impl;


import cn.hutool.core.collection.CollUtil;
import com.syy.api.system.mapper.PostMapper;
import com.syy.api.system.service.PostService;
import com.syy.common.enums.CommonStatusEnum;
import com.syy.system.domain.SysPost;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;

import javax.annotation.Resource;
import java.util.Collection;
import java.util.List;
import java.util.Map;

import static com.syy.common.enums.ErrorCodeConstants.POST_NOT_ENABLE;
import static com.syy.common.enums.ErrorCodeConstants.POST_NOT_FOUND;
import static com.syy.common.exception.ServiceExceptionUtil.exception;
import static com.syy.system.sms.CollectionUtils.convertMap;

/**
 * 岗位 Service 实现类
 *
 * @author 芋道源码
 */
@Service
@Validated
public class PostServiceImpl implements PostService {
    @Resource
    private PostMapper postMapper;
    @Override
    public void validatePostList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return;
        }
        // 获得岗位信息
        List<SysPost> posts = postMapper.selectBatchIds(ids);
        Map<Long, SysPost> postMap = convertMap(posts, SysPost::getPostId);
        // 校验
        ids.forEach(id -> {
            SysPost post = postMap.get(id);
            if (post == null) {
                throw exception(POST_NOT_FOUND);
            }
            if (!CommonStatusEnum.ENABLE.getStatus().equals(post.getStatus())) {
                throw exception(POST_NOT_ENABLE, post.getPostName());
            }
        });
    }
}
